package com.niiwoo.civet.trade.dto.kaola;

import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.trade.enums.kaola.ErrorCodeConstants;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import java.io.Serializable;

/**
 * @author daizhiyue
 * @ClassName: Result
 * @Description: 业务逻辑层的执行结果
 * @date 2017-12-25
 */
public class Result implements Serializable {
    private static final long serialVersionUID = 8923107164004809300L;

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public static final int SUCCESS_RET = 0;
    public static final String SUCCESS_RET_MSG = "操作成功。";
    public static final int FAIL_RET = -1;
    public static final String FAIL_RET_MSG = "操作失败。";

    //一般0代表成功
    private int ret;
    //错误信息
    private String message;
    //数据
    public Object data;

    private Exception exception;

    private String errorCode;

    public Result(int ret) {
        this.ret = ret;
    }

    public Result(int ret, Exception e) {
        this.ret = ret;
        this.exception = e;
    }

    public Result(int ret, String message) {
        this.ret = ret;
        this.message = message;
    }

    public Result(int ret, String message, String errorCode) {
        this.ret = ret;
        this.message = message;
        this.errorCode = errorCode;
    }

    public Result(int ret, String message, String errorCode, Exception e) {
        this.ret = ret;
        this.message = message;
        this.errorCode = errorCode;
        this.exception = e;
    }

    public Result(int ret, String message, Exception e) {
        this.ret = ret;
        this.message = message;
        this.exception = e;
    }

    public Result(int ret, String message, Object data) {
        this.ret = ret;
        this.message = message;
        this.data = data;
    }

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setMessage(String message, Object... params) {
        FormattingTuple ft = MessageFormatter.arrayFormat(message, params);
        this.message = ft.getMessage();
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    public Exception getException() {
        return exception;
    }


    public void setException(Exception exception) {
        this.exception = exception;
    }


    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public Result setFailureMessage(String message, Object... params) {
        FormattingTuple ft = MessageFormatter.arrayFormat(message, params);
        this.ret = FAIL_RET;
        this.message = ft.getMessage();
        return this;
    }

    public Result setFailureMessage(String message) {
        this.ret = FAIL_RET;
        this.message = message;
        return this;
    }

    public boolean isSuccess() {
        return this.ret == SUCCESS_RET;
    }

    public boolean isFail() {
        return !isSuccess();
    }

    /**
     * 系统错误统一结果实例
     *
     * @return
     */
    public static Result getSystemErrorResult() {
        return new Result(Result.FAIL_RET, ErrorCodeConstants.SYSTEM_ERROR_MSG, ErrorCodeConstants.SYSTEM_ERROR);
    }

    /**
     * 操作成功统一结果实例
     *
     * @return
     */
    public static Result getSuccessResult() {
        return new Result(Result.SUCCESS_RET, Result.SUCCESS_RET_MSG);
    }

}
